# !/usr/bin/python
# -*- coding: utf-8 -*-

# mediaplex
# multimedia platform
#
# this file is licensed under the MIT license
# read "LICENSE.MIT" in the root of this distribution

__authors__ = 'Jan Jokela <janjokela@gmail.com>'
__description__ = 'Logging interface'

import os
import logging

log = None

class Log():
    def __init__(self):
        """
        The basic configuration for logging
        """
        self.filename = os.path.join(os.path.expanduser('~'), '.mediaplex', 'mediaplex.log')
        self._verify_integrity()
        logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                            datefmt='%m-%d %H:%M',
                            filename=self.filename,
                            filemode='w')
        console = logging.StreamHandler()
        console.setLevel(logging.INFO)
        formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
        console.setFormatter(formatter)
        logging.getLogger('').addHandler(console)
    
    def _verify_integrity(self):
        if not os.path.exists(self.filename):
            f = open(self.filename, 'w')
            f.close()
        
    """
    def debug, info, warning, error, critical
    
    use these to log by different level
    these are implementations of the base levels
    
    :area (string) -- area in the application, or whatever is appropriate
    :msg (string) -- message to log
    """
    def debug(area, msg):
        logger = logging.getLogger(area)
        logger.debug(msg)
        
    def info(area, msg):
        logger = logging.getLogger(area)
        logger.info(msg)
        
    def warning(area, msg):
        logger = logging.getLogger(area)
        logger.warning(msg)
        
    def error(area, msg):
        logger = logging.getLogger(area)
        logger.error(msg)
            
    def critical(area, msg):
        logger = logging.getLogger(area)
        logger.critical(msg)
